package com.wg.androidsdk;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.unity3d.player.UnityPlayer;

import java.util.List;

public class BleConnent {

   BluetoothGattService bluetoothGattService;
   BluetoothGatt bluetoothGatt;
    //读特征
    private BluetoothGattCharacteristic readCharacteristic;

    //写特征
    private BluetoothGattCharacteristic writeCharacteristic;

    private ConnentListener connentListener; //连接监听


    public BleConnent(){

    }

    //设置监听
    public void setConnentListener(ConnentListener listener){
        connentListener = listener;
    }

    //蓝牙连接
    public void ConnectDevice(BluetoothDevice device, Context context){
     bluetoothGatt = device.connectGatt(context,false,bluetoothGattCallback);
    }

    private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyUpdate(gatt, txPhy, rxPhy, status);
        }

        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            super.onPhyRead(gatt, txPhy, rxPhy, status);
        }

        /***
         * 连接状态发生改变的回调
         * @param gatt
         * @param status
         * @param newState
         */
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            switch(status){
                case BluetoothGatt.GATT_SUCCESS:
                    Log.w("连接回调：","BluetoothGatt.GATT_SUCCESS");
                    connentListener.connentState("BluetoothGatt.GATT_SUCCESS");
                    //连接成功、去发现服务
                    gatt.discoverServices();
                    break;
                case BluetoothGatt.GATT_FAILURE:
                    Log.w("连接回调：","BluetoothGatt.GATT_FAILURE");
                    connentListener.connentState("BluetoothGatt.GATT_FAILURE");
                    break;
                case BluetoothGatt.GATT_CONNECTION_CONGESTED:
                    Log.w("连接回调：","BluetoothGatt.GATT_CONNECTION_CONGESTED");
                    break;
                case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
                    Log.w("连接回调：","BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION");
                    break;
                case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
                    Log.w("连接回调：","BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION");
                    break;
                case BluetoothGatt.GATT_INVALID_OFFSET:
                    Log.w("连接回调：","BluetoothGatt.GATT_INVALID_OFFSET");
                    break;
                case BluetoothGatt.GATT_READ_NOT_PERMITTED:
                    Log.w("连接回调：","BluetoothGatt.GATT_READ_NOT_PERMITTED");
                    break;
                case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
                    Log.w("连接回调：","BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED");
                    break;
            }

        }

        /***
         *
         * @param gatt
         * @param status
         */
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            Log.d("发现服务", "onServicesDiscovered: ");

            //配置服务信息
            for (BluetoothGattService service : gatt.getServices()){
                if (service.getUuid().toString().equals(Constant.SERVICE_UUID)){
                    bluetoothGattService = service;
                }
            }

            if (bluetoothGattService == null) {
                //找不到该服务就立即断开连接
                Log.e("", "setupService()-->bluetoothGattService == null");
            }

            //获取读写特征
            for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) {
                if (characteristic.getUuid().toString().equals(Constant.READ_UUID)) {  //读特征
                    readCharacteristic = characteristic;
                } else if (characteristic.getUuid().toString().equals(Constant.WRITE_UUID)) {  //写特征
                    writeCharacteristic = characteristic;
                }
            }

            if (readCharacteristic == null) {
                Log.e("TAG", "setupService()-->readCharacteristic == null");

            }

            if (writeCharacteristic == null) {
                Log.e("TAG", "setupService()-->writeCharacteristic == null");

            }
            //打开读通知
            gatt.setCharacteristicNotification(readCharacteristic,true);

            //重点中重点，需要重新设置
            List<BluetoothGattDescriptor> descriptors = readCharacteristic.getDescriptors();
            for (BluetoothGattDescriptor descriptor : descriptors) {
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                gatt.writeDescriptor(descriptor);
            }

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
        }

        /***
         * 写入回调
         * @param gatt
         * @param characteristic
         * @param status
         */
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            Log.d("", "写入回调!!");
            if(characteristic.getValue() == null){
                Log.e("TAG","characteristic.getValue() == null");
                return;
            }
            //将收到的字节数组转换成十六进制字符串
            String msg = TypeConversion.bytes2HexString(characteristic.getValue(),characteristic.getValue().length);
            if(status == BluetoothGatt.GATT_SUCCESS){
                //写入成功
                Log.w("TAG","写入成功：" + msg);

            }else if(status == BluetoothGatt.GATT_FAILURE){
                //写入失败
                Log.e("TAG","写入失败：" + msg);
            }else if(status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){
                //没有权限
                Log.e("TAG","没有权限！");
            }

        }

        /***
         * 接收数据回调
         * @param gatt
         * @param characteristic
         */
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            //接收数据
            byte[] bytes = characteristic.getValue();
            BytesWrapper bytesWrapper = new BytesWrapper();
            bytesWrapper.setBytes(bytes);
            Log.d("收到数据：",TypeConversion.bytes2HexString(bytes,bytes.length) );
//            connentListener.dataRead(TypeConversion.bytes2HexString(bytes,bytes.length));
            connentListener.dataRead(bytesWrapper);
        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorRead(gatt, descriptor, status);
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            super.onDescriptorWrite(gatt, descriptor, status);
        }

        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            super.onReliableWriteCompleted(gatt, status);
        }

        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            super.onReadRemoteRssi(gatt, rssi, status);
        }

        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            super.onMtuChanged(gatt, mtu, status);
        }
    };


    public void UnityDebug(BytesWrapper data){
        Log.d("UnityDebug: ", TypeConversion.bytes2HexString(data.getBytes(),data.getBytes().length));
    }

    /**
     * 发送消息  byte[]数组
     * @param msg  消息
     * @return  true  false
     */
    public boolean sendMessage(String msg){
        if(writeCharacteristic == null){
            Log.e("","sendMessage(byte[])-->writeGattCharacteristic == null");
            return false;
        }

        if(bluetoothGatt == null){
            Log.e("","sendMessage(byte[])-->mBluetoothGatt == null");
            return false;
        }

        boolean  b = writeCharacteristic.setValue(TypeConversion.hexString2Bytes(msg));
        Log.d("", "写特征设置值结果：" + b);
        return bluetoothGatt.writeCharacteristic(writeCharacteristic);
    }

}
